package com.yuezhu.control;

import com.android.volley.Response;
import com.yuezhu.been.UserMsg;
import com.yuezhu.com_volley.OkPostRequest;
import com.yuezhu.com_volley.OkRequestQueue;
import com.yuezhu.constante.LocalUrl;
import com.yuezhu.global.ApplicationContext;
import com.yuezhu.util.LogUtils;
import com.yuezhu.versionup.VersionCheckUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 任务引擎，所有业务接口封装 Created by zhaoshuzhen on 2015/12/8.
 */
public class TaskEngine {

    private static TaskEngine instance;
    private VersionCheckUtil util;

    public static synchronized TaskEngine getInstance() {

        if (instance == null) {
            instance = new TaskEngine();
        }
        return instance;
    }

    private String key = "689d3783957d65d57229ba3dc70a20fb";

    private TaskEngine() {
        util = VersionCheckUtil.getInstance(ApplicationContext.getContext(), null);
    }

    public static String getTokenParams(String name, String value) {
        return name + FLAG + value;
    }

    private static final String FLAG = "=";

    // 注册接口
    public void startRegistTask(OkRequestQueue queue, String userName, String userPwd, String userEmail,
                                Response.Listener listener, Response.ErrorListener errorListener) {
        String stampToken = System.currentTimeMillis() + "";
        Map<String, String> map = new HashMap<String, String>();
        map.put("userName", userName);
        map.put("userPwd", userPwd);
        map.put("userEmail", userEmail);
        map.put("stampToken", stampToken);
        String url = LocalUrl.REGIST + util.getBaseParams(map);
        LogUtils.e("url", url);
        OkPostRequest okPostRequest = new OkPostRequest(url,
                null, listener, errorListener);
        queue.add(okPostRequest);
    }

    // 登录接口
    public void startLoginTask(OkRequestQueue queue, String userName, String userPwd,
                               Response.Listener listener, Response.ErrorListener errorListener) {
        String stampToken = System.currentTimeMillis() + "";
        Map<String, String> map = new HashMap<String, String>();
        map.put("userName", userName);
        map.put("userPwd", userPwd);
        map.put("stampToken", stampToken);
        map.put("appId", "2016001");
        String url = LocalUrl.LOGIN + util.getBaseParams(map);
        LogUtils.e("url", url);
        OkPostRequest okPostRequest = new OkPostRequest(url,
                null, listener, errorListener);
        queue.add(okPostRequest);
    }

    // 获取项目列表
    public void startXiangMuTask(OkRequestQueue queue, String bldgType, String bldgName, String page,
                                 Response.Listener listener, Response.ErrorListener errorListener) {
        String stampToken = System.currentTimeMillis() + "";
        Map<String, String> map = new HashMap<String, String>();
        if (bldgType != null)
            map.put("bldgType", bldgType);
        if (bldgName != null)
            map.put("bldgName", bldgName);
        map.put("stampToken", stampToken);
        map.put("page", page);
        map.put("authToken", UserMsg.getInstance().getAuthToken());
        String url = LocalUrl.XIANGMU_LIST + util.getBase1Params(map);
        LogUtils.e("url", url);
        OkPostRequest okPostRequest = new OkPostRequest(url,
                null, listener, errorListener);
        queue.add(okPostRequest);
    }

    // 获取字典
    public void startDataTask(OkRequestQueue queue, Response.Listener listener, Response.ErrorListener errorListener) {
        String stampToken = System.currentTimeMillis() + "";
        Map<String, String> map = new HashMap<String, String>();
        map.put("stampToken", stampToken);
        map.put("authToken", UserMsg.getInstance().getAuthToken());
        String url = LocalUrl.DATA + util.getBase1Params(map);
        LogUtils.e("url", url);
        OkPostRequest okPostRequest = new OkPostRequest(url,
                null, listener, errorListener);
        queue.add(okPostRequest);
    }

    // 重新发送激活邮件
    public void startSendMailTask(OkRequestQueue queue, String authToken, Response.Listener listener, Response.ErrorListener errorListener) {
        String stampToken = System.currentTimeMillis() + "";
        Map<String, String> map = new HashMap<String, String>();
        map.put("stampToken", stampToken);
        map.put("authToken", authToken);
        String url = LocalUrl.SEND_MAIL + util.getSendMailBaseParams(map);
        LogUtils.e("url", url);
        OkPostRequest okPostRequest = new OkPostRequest(url,
                null, listener, errorListener);
        queue.add(okPostRequest);
    }

    // 添加项目
    public void startAddProjectTask(OkRequestQueue queue, Map<String, String> map, Response.Listener listener, Response.ErrorListener errorListener) {
        String stampToken = System.currentTimeMillis() + "";
        map.put("stampToken", stampToken);
        map.put("authToken", UserMsg.getInstance().getAuthToken());
        String url = LocalUrl.ADD_PROJECT + util.getBase1Params(map);
        LogUtils.e("url", url);
        OkPostRequest okPostRequest = new OkPostRequest(url,
                null, listener, errorListener);
        queue.add(okPostRequest);
    }

    // 获取城市数据
    public void startGetCityTask(OkRequestQueue queue, Response.Listener listener, Response.ErrorListener errorListener) {
        String stampToken = System.currentTimeMillis() + "";
        Map<String, String> map = new HashMap<>();
        map.put("stampToken", stampToken);
        map.put("authToken", UserMsg.getInstance().getAuthToken());
        String url = LocalUrl.GET_CITY + util.getBase1Params(map);
        LogUtils.e("url", url);
        OkPostRequest okPostRequest = new OkPostRequest(url,
                null, listener, errorListener);
        queue.add(okPostRequest);
    }

    // 更新项目
    public void startUpdateProjectTask(OkRequestQueue queue, Map<String, String> map, Response.Listener listener, Response.ErrorListener errorListener) {
        String stampToken = System.currentTimeMillis() + "";
        map.put("stampToken", stampToken);
        map.put("authToken", UserMsg.getInstance().getAuthToken());
        String url = LocalUrl.UPDATE_PROJECT + util.getBase1Params(map);
        LogUtils.e("url", url);
        OkPostRequest okPostRequest = new OkPostRequest(url,
                null, listener, errorListener);
        queue.add(okPostRequest);
    }

    // 获取分区信息
    public void startGetAreaTask(OkRequestQueue queue, Map<String, String> map, Response.Listener listener, Response.ErrorListener errorListener) {
        String stampToken = System.currentTimeMillis() + "";
        map.put("stampToken", stampToken);
        map.put("authToken", UserMsg.getInstance().getAuthToken());
        String url = LocalUrl.GET_AREA + util.getBase1Params(map);
        LogUtils.e("url", url);
        OkPostRequest okPostRequest = new OkPostRequest(url,
                null, listener, errorListener);
        queue.add(okPostRequest);
    }

    // 获取分区信息
    public void startDeleteProjectTask(OkRequestQueue queue, Map<String, String> map, Response.Listener listener, Response.ErrorListener errorListener) {
        String stampToken = System.currentTimeMillis() + "";
        map.put("stampToken", stampToken);
        map.put("authToken", UserMsg.getInstance().getAuthToken());
        String url = LocalUrl.DELETE_PROJECT + util.getBase1Params(map);
        LogUtils.e("url", url);
        OkPostRequest okPostRequest = new OkPostRequest(url,
                null, listener, errorListener);
        queue.add(okPostRequest);
    }

    // 获取项目信息
    public void startGetProjectTask(OkRequestQueue queue, Map<String, String> map, Response.Listener listener, Response.ErrorListener errorListener) {
        String stampToken = System.currentTimeMillis() + "";
        map.put("stampToken", stampToken);
        map.put("authToken", UserMsg.getInstance().getAuthToken());
        String url = LocalUrl.GET_PROJECT + util.getBase1Params(map);
        LogUtils.e("url", url);
        OkPostRequest okPostRequest = new OkPostRequest(url,
                null, listener, errorListener);
        queue.add(okPostRequest);
    }

    // 更新分区
    public void startUpdateAreaTask(OkRequestQueue queue, Map<String, String> map, Response.Listener listener, Response.ErrorListener errorListener) {
        String stampToken = System.currentTimeMillis() + "";
        map.put("stampToken", stampToken);
        map.put("authToken", UserMsg.getInstance().getAuthToken());
        String url = LocalUrl.UPDATE_AREA + util.getBase1Params(map);
        LogUtils.e("url", url);
        OkPostRequest okPostRequest = new OkPostRequest(url,
                null, listener, errorListener);
        queue.add(okPostRequest);
    }

    public void startGetUserInfoTask(OkRequestQueue queue, Response.Listener listener, Response.ErrorListener errorListener) {
        String stampToken = System.currentTimeMillis() + "";
        Map<String, String> map = new HashMap<String, String>();
        map.put("stampToken", stampToken);
        map.put("authToken", UserMsg.getInstance().getAuthToken());
        String url = LocalUrl.USER_INFO + util.getSendMailBaseParams(map);
        LogUtils.e("url", url);
        OkPostRequest okPostRequest = new OkPostRequest(url,
                null, listener, errorListener);
        queue.add(okPostRequest);
    }

    public void startCkeckUserNameTask(OkRequestQueue queue, String userName, Response.Listener listener, Response.ErrorListener errorListener) {
        String stampToken = System.currentTimeMillis() + "";
        Map<String, String> map = new HashMap<String, String>();
        map.put("stampToken", stampToken);
        map.put("data", userName);
        map.put("type", "userName");
        String url = LocalUrl.CHECK_DATA + util.getSendMailBaseParams(map);
        LogUtils.e("url", url);
        OkPostRequest okPostRequest = new OkPostRequest(url,
                null, listener, errorListener);
        queue.add(okPostRequest);
    }

    public void startCkeckUserMailTask(OkRequestQueue queue, String mail, Response.Listener listener, Response.ErrorListener errorListener) {
        String stampToken = System.currentTimeMillis() + "";
        Map<String, String> map = new HashMap<String, String>();
        map.put("stampToken", stampToken);
        map.put("data", mail);
        map.put("type", "userEmail");
        String url = LocalUrl.CHECK_DATA + util.getSendMailBaseParams(map);
        LogUtils.e("url", url);
        OkPostRequest okPostRequest = new OkPostRequest(url,
                null, listener, errorListener);
        queue.add(okPostRequest);
    }
}
